package com.okcl.manager.controller;

import com.github.pagehelper.PageInfo;
import com.okcl.manager.service.SysRoleService;
import com.okcl.model.dto.system.AssginMenuDto;
import com.okcl.model.dto.system.SysRoleDto;
import com.okcl.model.entity.system.SysRole;
import com.okcl.model.vo.common.Result;
import com.okcl.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Tag(name = "角色接口")
@RequestMapping("/admin/system/sysRole")
@RequiredArgsConstructor
public class SysRoleController {
    private final SysRoleService sysRoleService;

    /**
     * 分页查询角色列表
     *
     * @param sysRoleDto
     * @return
     */
    @Operation(summary = "分页查询角色列表")
    @PostMapping("/roleList")
    public Result list(@RequestBody SysRoleDto sysRoleDto) {
        PageInfo<SysRole> sysRoleList = sysRoleService.queryPageRoleList(sysRoleDto);
        return Result.build(sysRoleList, ResultCodeEnum.SUCCESS);
    }

    /**
     * 添加角色
     *
     * @param sysRole
     * @return
     */
    @Operation(summary = "添加角色")
    @PostMapping("/add")
    public Result save(@RequestBody SysRole sysRole) {
        sysRoleService.save(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改角色
     *
     * @param sysRole
     * @return
     */
    @Operation(summary = "修改角色")
    @PutMapping("/edit")
    public Result edit(@RequestBody SysRole sysRole) {
        sysRoleService.edit(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除角色")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id") Long id) {
        sysRoleService.delete(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 分配菜单
     *
     * @param assginMenuDto
     * @return
     */
    @Operation(summary = "分配菜单")
    @PostMapping("/assignMenu")
    public Result assignMenu(@RequestBody AssginMenuDto assginMenuDto) {
        sysRoleService.assignMenu(assginMenuDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询角色绑定菜单集合
     *
     * @param id 角色id
     * @return 菜单集合
     */
    @Operation(summary = "查询已分配的菜单")
    @GetMapping("/queryAssignedMenu/{id}")
    public Result queryAssignedMenu(@PathVariable(value = "id") Long id) {
        Map<String, Object> map = sysRoleService.queryAssignedMenu(id);
        return Result.build(map, ResultCodeEnum.SUCCESS);
    }

}
